const {app ,ipcMain,autoUpdater,mainWindow, BrowserWindow} = require('electron')
const path = require('path')
const url = require('url')
const appVersion = require('./package.json').version;
const os = require('os').platform();
const cp = require('child_process');
const ipc = window.require('electron').ipcRenderer;
ipc.on('backinfo', backInfo())
ipc.send('check-for-update');
let win
function backInfo(event,arg) {

}
//自动更新
//const autoUpdater = electron.autoUpdater
function startupEventHandle(){
    if(require('electron-squirrel-startup')) return;
    var handleStartupEvent = function () {
        if (process.platform !== 'win32') {
            return false;
        }
        var squirrelCommand = process.argv[1];
        switch (squirrelCommand) {
            case '--squirrel-install':
            case '--squirrel-updated':
                install();
                return true;
            case '--squirrel-uninstall':
                uninstall();
                app.quit();
                return true;
            case '--squirrel-obsolete':
                app.quit();
                return true;
        }
        // 安装
        function install() {
            var cp = require('child_process');
            var updateDotExe = path.resolve(path.dirname(process.execPath), '..', 'update.exe');
            var target = path.basename(process.execPath);
            var child = cp.spawn(updateDotExe, ["--createShortcut", target], { detached: true });
            child.on('close', function(code) {
                app.quit();
            });
        }
        // 卸载
        function uninstall() {
            var cp = require('child_process');
            var updateDotExe = path.resolve(path.dirname(process.execPath), '..', 'update.exe');
            var target = path.basename(process.execPath);
            var child = cp.spawn(updateDotExe, ["--removeShortcut", target], { detached: true });
            child.on('close', function(code) {
                app.quit();
            });
        }
    };
    if (handleStartupEvent()) {
        return ;
    }
}
function updateHandle(){

    //退出
    ipcMain.on('window-all-closed', () => {
        app.quit();
    });
//小化
    ipcMain.on('hide-window', () => {
        win.minimize();
    });
//最大化
    ipcMain.on('show-window', () => {
        win.maximize();
    });
//还原
    ipcMain.on('orignal-window', () => {
        win.unmaximize();
    });
    ipcMain.on('check-for-update', function(event, arg) {
        let appName='易店长';
        let appIcon=__dirname + '/assets/add.png';
        let message={
            error:'检查更新出错',
            checking:'正在检查更新……',
            updateAva:'updateAva',
            updateNotAva:'现在使用的就是最新版本，不用更新',
            downloaded:'最新版本已下载，将在重启程序后更新'
        };
        const os = require('os');
        const {dialog} = require('electron');
        //autoUpdater.setFeedURL('http://ailiyunlike.oss-cn-shanghai.aliyuncs.com/yunshopupdate/');
        autoUpdater.setFeedURL('http://yunlandyunshop.oss-cn-beijing.aliyuncs.com/yunshopupdate/');
        autoUpdater.on('error', function(error){

            return dialog.showMessageBox(win, {
                type: 'info',
                //icon: appIcon,
                buttons: ['OK'],
                title: appName,
                message: message.error,
                detail: '\r'+error
            });
        })
            .on('checking-for-update', function(e) {
                event.sender.send('backinfo',message.checking);
                // return dialog.showMessageBox(win, {
                //     type: 'info',
                //    // icon: appIcon,
                //     buttons: ['OK'],
                //     title: appName,
                //     message: message.checking
                // });
            })
            .on('update-available', function(e) {
                event.sender.send('backinfo',message.updateAva);
                // var downloadConfirmation = dialog.showMessageBox(win, {
                //     type: 'info',
                //     //icon: appIcon,
                //     buttons: ['OK'],
                //     title: appName,
                //     message: message.updateAva
                // });
                // if (downloadConfirmation === 0) {
                //     return;
                // }
            })
            .on('update-not-available', function(e) {
                event.sender.send('backinfo',message.updateNotAva);
                // return dialog.showMessageBox(win, {
                //     type: 'info',
                //    // icon: appIcon,
                //     buttons: ['OK'],
                //     title: appName,
                //     message: message.updateNotAva
                // });
            })
            .on('update-downloaded',  function (event, releaseNotes, releaseName, releaseDate, updateUrl, quitAndUpdate) {

                var index = dialog.showMessageBox(win, {
                    type: 'info',
                    //icon: appIcon,
                    buttons: ['现在重启','稍后重启'],
                    title: appName,
                    message: message.downloaded,
                    detail: releaseName + "\n\n" + releaseNotes
                });
                if (index === 1) return;
                autoUpdater.quitAndInstall();
            });
        autoUpdater.checkForUpdates();
    });
};
updateHandle();



function createWindow () {
    // Create the browser window.
    win = new BrowserWindow({ width: 1440
                              ,height: 960
                              , frame: false
                             ,preload: path.join(__dirname, 'dist/expose-window-apis.js')
        })
    win.abd=2;
    // and load the index.html of the app.
    win.loadURL(url.format({
        pathname: path.join(__dirname, 'dist/index.html'),
        protocol: 'file:',
        slashes: true
    }))
   // win.setSkipTaskbar(true)
    // Open the DevTools.
    //win.webContents.openDevTools()

    // Emitted when the window is closed.
    win.on('closed', () => {
        // Dereference the window object, usually you would store windows
        // in an array if your app supports multi windows, this is the time
        // when you should delete the corresponding element.
        win = null
    })
}

// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on('ready', createWindow)

// Quit when all windows are closed.
app.on('window-all-closed', () => {
    // On macOS it is common for applications and their menu bar
    // to stay active until the user quits explicitly with Cmd + Q
    if (process.platform !== 'darwin') {
        app.quit()
    }
})

app.on('activate', () => {
    // On macOS it's common to re-create a window in the app when the
    // dock icon is clicked and there are no other windows open.
    if (win === null) {
        createWindow()
    }
})

// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and require them here.